import requests
from lxml import etree
from 爬取导师的每一页个人信息链接 import get_teacher_url
from 爬取姓名 import Name


Shumei_Name=Name("https://yzb.bupt.edu.cn/content/content.php?p=8_4_74")

# 首先，得到全部研究生导师的名字
Shumei_all_yanjiushengtea_name=Shumei_Name.check_name()

#得到全部导师的名字


s1=Name("https://sdmda.bupt.edu.cn/szdw/js.htm")
ss1=s1.get_all_name('//div[@class="name-group"]/span[1]/text()')

s2=Name("https://sdmda.bupt.edu.cn/szdw/fjs.htm")
ss2=s2.get_all_name('//div[@class="name-group"]/span[1]/text()')

s3=Name("https://sdmda.bupt.edu.cn/szdw/js1.htm")
ss3=s3.get_all_name('//div[@class="name-group"]/span[1]/text()')

s4=Name("https://sdmda.bupt.edu.cn/szdw/xz.htm")
ss4=s4.get_all_name('//div[@class="name-group"]/span[1]/text()')

s5=Name("https://sdmda.bupt.edu.cn/szdw/jf.htm")
ss5=s5.get_all_name('//div[@class="name-group"]/span[1]/text()')

Shumei_allteacher_name=ss1+ss2+ss3+ss4+ss5


#得到所有导师的链接
Teaher_url=get_teacher_url("https://sdmda.bupt.edu.cn/szdw/js.htm")
e1=Teaher_url.get_url('//div[@class="row"]/a/@href')


#将导师名字与链接进行匹配
dict1=Teaher_url.each_real_url(ss1,e1)


Teaher_url=get_teacher_url("https://sdmda.bupt.edu.cn/szdw/fjs.htm")
e2=Teaher_url.get_url('//div[@class="row"]/a/@href')


#将导师名字与链接进行匹配
dict2=Teaher_url.each_real_url(ss2,e2)


Teaher_url=get_teacher_url("https://sdmda.bupt.edu.cn/szdw/js1.htm")
e3=Teaher_url.get_url('//div[@class="row"]/a/@href')
dict3=Teaher_url.each_real_url(ss3,e3)


Teaher_url=get_teacher_url("https://sdmda.bupt.edu.cn/szdw/xz.htm")
e4=Teaher_url.get_url('//div[@class="row"]/a/@href')
dict4=Teaher_url.each_real_url(ss4,e4)


Teaher_url=get_teacher_url("https://sdmda.bupt.edu.cn/szdw/jf.htm")
e5=Teaher_url.get_url('//div[@class="row"]/a/@href')
dict5=Teaher_url.each_real_url(ss5,e5)

Shumei_allteachers_urls=e1+e2+e3+e4+e5  #所有老师的链接

dict1.update(dict5)
dict1.update(dict4)
dict1.update(dict3)
dict1.update(dict2)
  #dict ,得到了所有字典的对应

#接下来进行名字选择

Final_dict={}
#现在进行匹配，得到了数媒导师的个人信息链接
for shumeiyanjiusheng_teacher in Shumei_all_yanjiushengtea_name:
    for name in Shumei_allteacher_name:
        if name==shumeiyanjiusheng_teacher:
            try:

              Final_dict[name]=dict1[name]

            except:
              Final_dict[name]=''

              Final_dict[name] =dict1[name]
              print( "再来")



#现在将每一个研究生导师的个人链接拿来作为url

allinfornation=[]   #是所有研究生导师的个人信息
Picture=[]       #导师的图片
for url in Final_dict.values():
# url是每一个研究生导师的个人链接
    headers = {
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'max-age=0',
        'cookie': 'Hm_lvt_c3acb27768b401b6598a1ae2797371a4=1693548649,1693581545; '
                  'Hm_lpvt_c3acb27768b401b6598a1ae2797371a4=1693581548',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/'
                      '537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/'
                      '537.36 SLBrowser/7.0.0.6251 SLBChan/124'}

    # 获取响应,得到响应数据
    response = requests.get(url=url, headers=headers)
    response.encoding='utf8'
    content=response.text
    #使用xpath
    tree=etree.HTML(content)
    r=tree.xpath('//div[@class="col-md-10 col-sm-10 col-xs-12"]/p/text()')
    if len(r) !=0:
        allinfornation.append(r)
    #弄图片

    picturee=etree.HTML(content)
    pict=tree.xpath('//div[@class="col-md-2 col-sm-2 hidden-xs"]/a/img/@src')
    if len(pict) != 0:
        Picture.append(pict)


application=[]   #这是老师的个人简介列表
for i in allinfornation:
    application.append(i[:-1])

Email=[]         #这是老师的邮箱列表
for i in allinfornation:
    Email.append(i[-1:])

Picture_url=[]   #这是老师的照片
for picture in Picture:
    picture_url='https://sdmda.bupt.edu.cn'+picture[0]
    Picture_url.append(picture_url)


i=0
while i<len(application):
    print(application[i])
    print(Email[i])
    print(Picture_url[i])
    print('\n')
    i+=1